import axios, { AxiosInstance } from "axios";
import { setupInterceptors } from "./interceptors";
import { AxiosBaseConfig } from "./model";
import { setAxiosBaseConfig } from "./config";

const instances: Map<string,AxiosInstance> = new Map();

export const createAxiosInstance = (config:AxiosBaseConfig) => {
  if (instances.has(config.baseURL)) {
    return instances.get(config.baseURL);
  }
  const instance = axios.create({
    ...config,
      baseURL:config.baseURL,
  });
  setAxiosBaseConfig(config);
  setupInterceptors(instance);
  instances.set(config.baseURL,instance)
  return instance;
};
export const getInstance = (baseURL: string): AxiosInstance | null => {
  if (instances.has(baseURL)) {
    return instances.get(baseURL)!;
  }
  return null;
};




